﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Media;
using TheCollectiveNetwork.Web.Services;
using TheCollectiveNetwork.Classes.Security;
using TheCollectiveNetwork.Views;
using TheCollectiveNetwork.CustomControls;
using TheCollectiveNetwork.Classes.GlobalConstantsVariables;
using TCNDataAccessLayer;


namespace TheCollectiveNetwork.Views.Administration
{
    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class UserManagement : Page
    {

        // Define Variables
        UserManagementContext _umc;

        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public UserManagement()
        {

            // Ensure that only admin users can access this page
            if (TCNauthentication.isAdminUser)
            {
                InitializeComponent();
                _umc = new UserManagementContext();
            }


        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }


        /* ------------  Button Clicks -------------- */
        private void UserSearchBtn_Click(object sender, EventArgs e)
        {
            LoadOperation<DTOAccountsUser> loadOp = _umc.Load<DTOAccountsUser>(_umc.Accounts_SearchQuery(usersearchtextbox.Text));

            loadOp.Completed += (s, ea) =>
            {
                UserListDataPager.Source = new PagedCollectionView(loadOp.Entities);
                UserListDataGrid.ItemsSource = UserListDataPager.Source;
            };


        }

        private void UserEditBtn_Click(object sender, EventArgs e)
        {
            GuidButton gbtn;
            gbtn = (GuidButton)sender;        
            PersonDetailsWindow pdw;

            pdw = new PersonDetailsWindow();            
            pdw.ShowPersonDetailsEdit(gbtn.GuidID, _umc);

            
            pdw.Closed += (s, eargs) => {
                // Ensure that the parent is enabled. Child window seems to not always renable properly when it closes
                Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);

                // Refresh the record list if a record was modified
                if (pdw.RecordModified)
                {
                    DTOAccountsUser_Refresh();
                };
                
            }; 
           
        }


        private void UserDeleteBtn_Click(object sender, EventArgs e)
        {
            GuidButton gbtn;
            gbtn = (GuidButton)sender;
            PersonDetailsWindow pdw;

            pdw = new PersonDetailsWindow();
            pdw.ShowPersonDetailsDelete(gbtn.GuidID, _umc);

            pdw.Closed += (s, eargs) =>
            {
                // Ensure that the parent is enabled. Child window seems to not always renable properly when it closes
                Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);

                // Refresh the record list if a record was modified
                if (pdw.RecordModified)
                {
                    DTOAccountsUser_Refresh();
                };

            }; 

        }

        /* ---------  Private Functions ------------ */

        // Refreshses the DTOAccountsUser Entity set after it has been changed
        private void DTOAccountsUser_Refresh()
        {
            LoadOperation<DTOAccountsUser> loadOp = _umc.Load<DTOAccountsUser>(_umc.Accounts_SearchQuery(usersearchtextbox.Text),
                                          LoadBehavior.RefreshCurrent,
                                          loadOperation =>
                                          {
                                              var results = _umc.DTOAccountsUsers.Where(entity => !loadOperation.Entities.Contains(entity)).ToList();
                                              results.ForEach(entity => { _umc.DTOAccountsUsers.Detach(entity); });
                                          }, null);
        }



       



    }
}